<template>
  <div class="infomanage-box">
    <el-card class="box-card">
        <div style="width: 100%;height: 30px;text-align:left;margin-bottom: 30px;">
			<img style="width:40px;height: 100%;" class="back-img" @click="goBack()" src="../../assets/info/back.svg" alt="">
		</div>
        <el-form :model="newForm" style="font-weight: 700; text-align: left; width: 100%; margin-top: 20px">
            <el-form-item label="标题">
                <el-input v-model="newForm.title" clearable maxlength="50"></el-input>
            </el-form-item>
            <el-form-item label="类型">
                <el-radio-group v-model="category">
                    <el-radio label="新闻"></el-radio>
                    <el-radio label="通知"></el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="内容">
                <el-input class="tabIndent" v-model="newForm.content" type="textarea" rows="15" clearable></el-input>
            </el-form-item>
            <el-form-item>
				<el-button style="margin-left: 3%;float: left">清空</el-button>
                <el-button style="float:left" type="primary" @click="releaseInfo()">发布</el-button>
				<span style="float: left;color: #ff0000;margin-left: 3%">{{this.notice}}</span>
            </el-form-item>
        </el-form>
		<el-dialog
			v-model="released"
			title="发布成功"
			width="30%"
		>
			<div style="width: 50%; height: 70%;margin: auto">
				<img style="width:100%;height: 100%" src="../../assets/info/released.png" >
			</div>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="goBack()">返回</el-button>
					<el-button type="primary" @click="released = false">继续发布</el-button>
				</span>
			</template>
		</el-dialog>
    </el-card>
  </div>
</template>

<script>

import {ElMessage } from 'element-plus'

import request from '../../utils/request'

export default {
	data() {
		return {
			newForm: {
				title: '',
				content: ''
			},
			category: '',
			released: false,
			notice: ''
		}
	},
	created() {
	},
	methods: {
		goBack() {
			window.history.back(-1);
		},
		// 文章：新闻，通知发布
		releaseInfo() {
			if (this.newForm.title === '') { // 判断标题是否为空
				this.notice = '请输入标题!';
				setTimeout(() => {
					this.notice = '';
				}, 2000);
			} else if (this.newForm.content === '') { // 为输入文章内容
				this.notice = '请输入内容！';
				setTimeout(() => {
					this.notice = '';
				}, 2000);
			} else if (this.category === '') { // 未选择发布类型提示
				this.notice = '请选择发布类型！';
				setTimeout(() => {
					this.notice = '';
				}, 2000);
			} else if (this.newForm.title.length > 50) {
				this.notice = '标题过长！不得多于50字';
				setTimeout(() => {
					this.notice = '';
				}, 2000);
			} else if (this.category === '新闻') { // 文章为新闻
				request({
					method: 'post',
					url: '/api/school-information/push',
					data: this.newForm
				}).then(res => {
					if (res.code === 200) {
						this.released = true;
						this.newForm.title = '';
						this.newForm.content = '';
					} else {
						ElMessage.error('发布失败！')
					}
				})
			} else if (this.category === '通知') { // 文章为通知
				request({
					method: 'post',
					url: '/api/report/push',
					data: this.newForm
				}).then(res => {
					if (res.code === 200) {
						this.released = true;
						this.newForm.title = '';
						this.newForm.content = '';
					} else {
						ElMessage.error('发布失败！')
					}
				})
			}
		}
	}
}
</script>

<style lang="less" scoped>
.infomanage-box {
    width: 100%;
    height: 100%;
}

.box-card {
    width: 100%;
    height: 100%;
}

.back-img:hover {
	cursor: pointer;
}
</style>
